﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SqlLite;

namespace SqliteDemo
{

    public class DownloadFile
    {
        public string Path { get; set; }
        public string LocalPath { get; set; }
        public string FileName
        {
            get;
            set;
        }
        public string Title { get; set; }
        public double LastModifyTime { get; set; }
        public string Bytes { get; set; }
        public string FileID { get; set; }
        public string DownloadURL { get; set; }
        public bool IsDir { get; set; }

        public static DownloadFile Derialize(object[] arr)
        {
            DownloadFile file = new DownloadFile();
            if (arr == null || arr.Length < 3) return null;
            file.Path = arr[0].ToString();
            file.Bytes = arr[1].ToString();
            file.LastModifyTime = (double)arr[2];
            return file;
        }

    }


    /// <summary>
    /// 
    /// </summary>
    public static class TimeFunction
    {
        /// <summary>
        /// double to datetime
        /// </summary>
        /// <param name="d">double</param>
        /// <returns>DateTime</returns>
        public static System.DateTime ConvertIntDateTime(double d)
        {
            System.DateTime time = System.DateTime.MinValue;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            time = startTime.AddSeconds(d);
            return time;
        }

        /// <summary>
        /// datetime to double
        /// </summary>
        /// <param name="time">datetime</param>
        /// <returns>double</returns>
        public static double ConvertDateTimeInt(System.DateTime time)
        {
            double intResult = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            intResult = (time - startTime).TotalSeconds;
            return intResult;
        }
    }

    public class DownloadFileDataProvider
    {
        const string dbName = "backup.db";
        const string insertSQL = "insert into tDBItemFile (path,targetPath,name,size,anchor) values(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\");";
        const string selectAllSQL = "select distinct path,size,anchor from tDBItemFile;";

        string dataSource = string.Empty;
        Dictionary<string, DownloadFile> dictFiles = new Dictionary<string, DownloadFile>();

        public void init(string dataFolder)
        {
            if (!Directory.Exists(dataFolder))
                Directory.CreateDirectory(dataFolder);

            string dataPath = string.Empty;
            if (dataFolder.EndsWith(@"\"))
                dataPath = dataFolder + dbName;
            else
                dataPath = dataFolder + @"\" + dbName;

            if (!File.Exists(dataPath))
            {
                //copy from install path
            }

            dataSource = dataPath;
        }

        public void ReadDatas()
        {
            try
            {
                if (!File.Exists(dataSource)) return;
                using (SqlLiteHelper sqlite = new SqlLiteHelper(dataSource))
                {
                    var array = sqlite.GetArray(selectAllSQL);
                    var files = array.Select(o => DownloadFile.Derialize(o)).Where(o => o != null);
                    if (files.FirstOrDefault() != null)
                    {
                        foreach (var file in files)
                        {
                            if (!dictFiles.ContainsKey(file.Path))
                            {
                                dictFiles.Add(file.Path, file);
                            }
                        }
                    }
                    else
                        return;
                }
            }
            catch { }
        }

        /// <summary>
        /// write  data
        /// </summary>
        /// <param name="file"></param>
        public void WriteData(DownloadFile file)
        {
            try
            {
                if (file == null) return;
                string sql = string.Format(insertSQL, file.Path, file.Path, file.FileName, file.Bytes, file.LastModifyTime);
                using (SqlLiteHelper sqlite = new SqlLiteHelper(dataSource))
                {
                    sqlite.ExeSql(sql);
                }
            }
            catch { }
        }

        /// <summary>
        /// write all
        /// </summary>
        /// <param name="files"></param>
        public void WriteDatas(List<DownloadFile> files)
        {
            StringBuilder builder = new StringBuilder();
            foreach (var file in files)
            {
                builder.AppendFormat(insertSQL, file.Path, file.Path, file.FileName, file.Bytes, file.LastModifyTime);
            }

            try
            {
                using (SqlLiteHelper sqlite = new SqlLiteHelper(dataSource))
                {
                    sqlite.ExeSql(builder.ToString());
                }
            }
            catch { }
        }
    }
}
